/*
Copyright 2014, KISSY v1.47
MIT Licensed
build time: May 22 12:29
*/
KISSY.add("mvc/sync",["io","json"],function(d,h){var j=h("io"),a=h("json"),b={create:"POST",update:"POST","delete":"POST",read:"GET"};return function(c,e,f){var f=d.merge({type:b[e],dataType:"json"},f),k,g;k=f.data=f.data||{};k._method=e;f.url||(g=c.get("url"),f.url="string"===typeof g?g:g.call(c));if("create"===e||"update"===e)k.model=a.stringify(c.toJSON());return j(f)}});
KISSY.add("mvc/model",["attribute"],function(d,h){var j="idAttribute,destroyed,plugins,listeners,clientId,urlRoot,url,parse,sync".split(",");return h("attribute").extend({getCollections:function(){return this.collections||(this.collections={})},addToCollection:function(a){this.getCollections()[d.stamp(a)]=a;this.addTarget(a)},removeFromCollection:function(a){delete this.getCollections()[d.stamp(a)];this.removeTarget(a)},getId:function(){return this.get(this.get("idAttribute"))},setId:function(a){return this.set(this.get("idAttribute"),
a)},setInternal:function(){this.__isModified=1;return this.callSuper.apply(this,arguments)},isNew:function(){return!this.getId()},isModified:function(){return!(!this.isNew()&&!this.__isModified)},destroy:function(a){var b=this,a=a||{},c=a.success;a.success=function(e){var f=b.getCollections();if(e){var d=b.get("parse").call(b,e);d&&b.set(d,a)}for(var g in f)f[g].remove(b,a);b.fire("destroy");c&&c.apply(b,arguments)};!b.isNew()&&a["delete"]?b.get("sync").call(b,b,"delete",a):(a.success(),a.complete&&
a.complete());return b},load:function(a){var b=this,a=a||{},c=a.success;a.success=function(e){if(e){var f=b.get("parse").call(b,e);f&&b.set(f,a)}b.__isModified=0;c&&c.apply(b,arguments)};b.get("sync").call(b,b,"read",a);return b},save:function(a){var b=this,a=a||{},c=a.success;a.success=function(e){if(e){var f=b.get("parse").call(b,e);f&&b.set(f,a)}b.__isModified=0;c&&c.apply(b,arguments)};b.get("sync").call(b,b,b.isNew()?"create":"update",a);return b},toJSON:function(){var a=this.getAttrVals();d.each(j,
function(b){delete a[b]});return a}},{ATTRS:{idAttribute:{value:"id"},clientId:{valueFn:function(){return d.guid("mvc-client")}},url:{value:function(){var a,b,c=this.getCollections();for(a in c)if(c.hasOwnProperty(a)){b=c[a];break}a=b;var e;a=(a&&(e=a.get("url"))?"string"===typeof e?e:e.call(a):e)||this.get("urlRoot");if(this.isNew())return a;a+="/"===a.charAt(a.length-1)?"":"/";return a+encodeURIComponent(this.getId())+"/"}},urlRoot:{value:""},sync:{value:function(){d.require("mvc").sync.apply(this,
arguments)}},parse:{value:function(a){return a}}}})});
KISSY.add("mvc/view",["node","attribute"],function(d,h){var j=h("node"),a=h("attribute"),b=j.all;return a.extend({constructor:function(){this.callSuper.apply(this,arguments);var a;(a=this.get("events"))&&this._afterEventsChange({newVal:a})},_afterEventsChange:function(a){var b=a.prevVal;b&&this._removeEvents(b);this._addEvents(a.newVal)},_removeEvents:function(a){var b=this.get("el"),f;for(f in a){var d=a[f],g;for(g in d)b.undelegate(g,f,"string"===typeof d[g]?this[d[g]]:d[g],this)}},_addEvents:function(a){var b=
this.get("el"),f;for(f in a){var d=a[f],g;for(g in d)b.delegate(g,f,"string"===typeof d[g]?this[d[g]]:d[g],this)}},render:function(){return this},destroy:function(){this.get("el").remove()}},{ATTRS:{el:{value:"<div />",getter:function(a){"string"===typeof a&&(a=b(a),this.setInternal("el",a));return a}},events:{}}})});
KISSY.add("mvc/collection",["./model","attribute"],function(d,h){var j=h("./model");return h("attribute").extend({sort:function(){var a=this.get("comparator");a&&this.get("models").sort(function(b,c){return a(b)-a(c)})},toJSON:function(){return d.map(this.get("models"),function(a){return a.toJSON()})},add:function(a,b){var c=this,e=!0;if(d.isArray(a)){var f=[].concat(a);d.each(f,function(a){a=c._add(a,b);e=e&&a})}else e=c._add(a,b);return e},remove:function(a,b){var c=this;if(d.isArray(a)){var e=
[].concat(a);d.each(e,function(a){c._remove(a,b)})}else a&&c._remove(a,b)},at:function(a){return this.get("models")[a]},_normModel:function(a){var b=!0;a instanceof j||(b=a,a=new (this.get("model")),b=a.set(b,{silent:1}));return b&&a},load:function(a){var b=this,a=a||{},c=a.success;a.success=function(e){if(e){var f=b.get("parse").call(b,e);f&&b.set("models",f,a)}d.each(b.get("models"),function(a){a.__isModified=0});c&&c.apply(b,arguments)};b.get("sync").call(b,b,"read",a);return b},create:function(a,
b){var c=this,b=b||{};if(a=this._normModel(a)){a.addToCollection(c);var e=b.success;b.success=function(){c.add(a,b);e&&e()};a.save(b)}return a},_add:function(a,b){if(a=this._normModel(a)){var b=b||{},c=this.get("models"),e=this.get("comparator"),f=c.length;if(e)for(var d=e(a),f=0;f<c.length;f++){var g=e(c[f]);if(d<g)break}this.get("models").splice(f,0,a);a.addToCollection(this);b.silent||this.fire("add",{model:a})}return a},_remove:function(a,b){var b=b||{},c=d.indexOf(a,this.get("models"));-1!==
c&&(this.get("models").splice(c,1),a.removeFromCollection(this));b.silent||this.fire("remove",{model:a})},getById:function(a){for(var b=this.get("models"),c=0;c<b.length;c++){var e=b[c];if(e.getId()===a)return e}return null},getByCid:function(a){for(var b=this.get("models"),c=0;c<b.length;c++){var e=b[c];if(e.get("clientId")===a)return e}return null}},{ATTRS:{model:{value:j},models:{setter:function(a){this.remove(this.get("models"),{silent:1});this.add(a,{silent:1});return this.get("models")},value:[]},
url:{value:""},comparator:{},sync:{value:function(){d.require("mvc").sync.apply(this,arguments)}},parse:{value:function(a){return a}}}})});
KISSY.add("mvc/router",["attribute","node"],function(d,h){function j(a){var b,c;for(c=0;c<a.length;c++)if(b=a.charAt(c),"\\"===b)c++;else if("("===b)return c;throw Error("impossible to not to get capture group in kissy mvc route");}function a(a){a=a||location.href;if(i.nativeHistory&&n){var a=new d.Uri(a),b=a.getQuery().toString();return a.getPath().substr(i.urlRoot.length)+(b?"?"+b:"")}return(new d.Uri(a)).getFragment().replace(/^!/,"")}function b(a){d.endsWith(a,"/")&&(a=a.substring(0,a.length-
1));return a}function c(a){a&&(d.startsWith(a,"/")&&(a=a.substring(1)),a="/"+a);return a}function e(a,c){a=b(a);c=b(c);return a===c}function f(){var b,c,e=0,f=-1,g="",h=-1,i=0,t="";b=new d.Uri(a());var k=0;c=b.clone();c.query.reset();c=c.toString();o(l,function(a){var b=0;o(a[p],function(d){var s=d.regStr,n=d.paramNames,l=-1,m,p=d.name,q=d.callback;if(m=c.match(d.reg)){m.shift();var r=function(){if(n){var a={};o(m,function(b,c){a[n[c]]=b});return a}return[].concat(m)},d=function(){g=s;h=l;i=q;k=r();
e=a;t=p;f=m.length};if(m.length)if(s)l=j(s),l>h?d():l===h&&f>=m.length?m.length<f?d():s.length>g.length&&d():e||d();else return d(),b=1,!1;else return d(),b=1,!1}});if(b)return!1});k&&(b=b.query.get(),i.apply(e,[k,b,{path:c,url:location.href}]),b={name:t,paths:k,path:c,url:location.href,query:b},e.fire("route:"+t,b),e.fire("route",b))}function k(a,b,c){var e=b,f=[];return"function"===typeof c?(b=d.escapeRegExp(b),b=b.replace(y,function(a,b,c,d,e){f.push(c||e);if(c)return"([^/]+)";if(e)return"(.*)"}),
{name:e,paramNames:f,reg:RegExp("^"+b+"$"),regStr:b,callback:c}):{name:e,reg:c.reg,callback:g(a,c.callback)}}function g(a,b){return"function"!==typeof b&&"string"===typeof b?a[b]:b}function u(a){this[p]={};this.addRoutes(a.newVal)}var z=h("attribute"),v=h("node"),o=d.each,y=/(:([\w\d]+))|(\\\*([\w\d]+))/g,l=[],w=d.Env.host,A=v.all,q=A(w),x=d.UA.ieMode,r=w.history,n=!(!r||!r.pushState),p="__routerMap",i;return i=z.extend({constructor:function(){this.callSuper.apply(this,arguments);this.on("afterRoutesChange",
u,this);u.call(this,{newVal:this.get("routes")});l.push(this)},addRoutes:function(a){var b=this;o(a,function(a,c){b[p][c]=k(b,c,g(b,a))})}},{ATTRS:{routes:{}},hasRoute:function(a){var b=0;o(l,function(c){o(c[p],function(c){if(a.match(c.reg))return b=1,!1});if(b)return!1});return!!b},removeRoot:function(a){return(new d.Uri(a)).getPath().substr(i.urlRoot.length)},navigate:function(d,e){var e=e||{},g=e.replaceHistory,h;a()!==d?i.nativeHistory&&n?(r[g?"replaceState":"pushState"]({},"",location.protocol+
"//"+location.host+b(i.urlRoot)+c(d)),f()):(h="#!"+d,g?location.replace(h+(x&&8>x?v.REPLACE_HISTORY:"")):location.hash=h):e&&e.triggerRoute&&f()},start:function(d){d=d||{};if(i.__started)return d.success&&d.success();d.urlRoot=(d.urlRoot||"").replace(/\/$/,"");var g,h=d.nativeHistory,j=location.pathname,k=a(),l=location.hash.match(/#!.+/);g=i.urlRoot=d.urlRoot;if(i.nativeHistory=h)if(n)l&&e(j,g)&&(r.replaceState({},"",location.protocol+"//"+location.host+b(i.urlRoot)+c(k)),d.triggerRoute=1);else if(!e(j,
g)){location.replace(b(g)+"/#!"+k);return}setTimeout(function(){if(h&&n)q.on("popstate",f);else q.on("hashchange",f),d.triggerRoute=1;d.triggerRoute&&f();d.success&&d.success()},100);i.__started=1},stop:function(){i.__started=0;q.detach("popstate",f);q.detach("hashchange",f);l=[]}})});KISSY.add("mvc",["mvc/sync","mvc/model","mvc/view","mvc/collection","mvc/router"],function(d,h){return{sync:h("mvc/sync"),Model:h("mvc/model"),View:h("mvc/view"),Collection:h("mvc/collection"),Router:h("mvc/router")}});
